package com.hs.auth.action;
 
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hs.auth.model.HsAccount;
import com.hs.auth.model.HsOrg;
import com.hs.auth.service.IOrgService;
import com.hs.core.action.BaseAction;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
import com.hs.core.utils.TimeUtil;
@Controller  
public class OrgController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(OrgController.class);
	private IOrgService orgService;
	 @Autowired  
	public void setOrgService(IOrgService orgService) {
		this.orgService = orgService;
	}
	 /**
	  * 组织机构信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/orgInfo.do", method = RequestMethod.GET)   
     public ModelAndView orgInfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String orgid = request.getParameter("orgid");//是否存在此参数表明是新增还是修改
          
         if(!StringUtils.isNotEmpty(orgid)){
        	 HsOrg org = (HsOrg)orgService.findByID(HsOrg.class, new Long(orgid));
        	 modelMap.put("obj", org);
         }
        
		 return new ModelAndView("",modelMap);
		 
	 }
	 /**
	  * 组织机构信息保存
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/saveOrg.do", method = RequestMethod.POST)   
     public ModelAndView saveOrg(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String orgid = request.getParameter("orgid");//是否存在此参数表明是新增还是修改
         String orgname = request.getParameter("orgname");
         String parentId = request.getParameter("parentId");
         String orgcode = request.getParameter("orgcode");
         String postaddress = request.getParameter("postaddress");
         String postnum = request.getParameter("postnum");
         HsAccount acc = this.getCurrentuser(request);
         Long currentTime = new Long(TimeUtil.getCurrentDate());
         HsOrg org = null;
         if(!StringUtils.isNotEmpty(orgid)){
        	 org = (HsOrg)orgService.findByID(HsOrg.class, new Long(orgid));
        	
         }else{
        	 org = new HsOrg();
         }
         org.setOrgcode(orgcode);
         org.setOrgname(orgname);
         org.setPid(new Long(parentId));
         org.setPostadress(postaddress);
         org.setPostnum(postnum);
         orgService.saveObject(org);
         modelMap.put("obj", org);
         return new ModelAndView("",modelMap);
		 
	 }
	 /**
	  * 组织机构列表
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/listOrg.do", method = RequestMethod.GET)   
     public ModelAndView listOrg(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    PageParam pageParam = new  PageParam(request,"data/listData.do");
			StringBuffer buffer = new StringBuffer();
			 
//		    pageParam.getConditionMap().put("orderName", orderName);
//		    pageParam.getConditionMap().put("orderNum", orderNum);
		    
		    //排序结束
			PageUtil pageUtil = null;
			StringBuffer searchBuffer = new StringBuffer(" select a from HsOrg a where a.isdelete='0'  ");
			String prename = request.getParameter("prename"); 
			String precomfrom = request.getParameter("precomfrom"); 
			String pstatus = request.getParameter("pstatus"); 
			String dbsourse = request.getParameter("dbsourse"); 

			 
			if(StringUtils.isNotBlank(prename)){
				searchBuffer.append(" and a.prename like ?");
				pageParam.getConditionMap().put("prename", prename);
				request.setAttribute("prename", prename);
			}  
			 
			 searchBuffer.append(" order by a.prename,a.precomfrom");
			 
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = orgService.getOrgList(pageParam);
			modelMap.put("dataList", pageUtil);   
			//List dbsList = dataService.findList("select a  from PdmsDsConfig a ");
	 		//data.put("dbslist", dbsList);
			return new ModelAndView("../../app/data/dataList",modelMap);   
		 
	 }
	  
	 
	 
}
